//
// Created by 17328 on 2025/8/27.
//
#include "utils.h"
#include <cctype>
#include <algorithm>
#include <sys/stat.h>
#include <filesystem>

namespace StringUtils {
    std::string trim(std::string s) {
        auto issp = [](int ch){ return std::isspace(ch); };
        while (!s.empty() && issp(s.front())) s.erase(s.begin());
        while (!s.empty() && issp(s.back())) s.pop_back();
        return s;
    }

    std::string toLower(const std::string& s) {
        std::string result = s;
        std::transform(result.begin(), result.end(), result.begin(), ::tolower);
        return result;
    }

    std::string toUpper(const std::string& s) {
        std::string result = s;
        std::transform(result.begin(), result.end(), result.begin(), ::toupper);
        return result;
    }
}

namespace FileUtils {
    bool isExist(const std::string& filePath) {
        return !filePath.empty() && std::filesystem::exists(filePath);
    }
}

namespace DateUtils {
    std::string getCurrentTime(const std::string& format) {
        auto now = std::chrono::system_clock::now();
        std::time_t current_time = std::chrono::system_clock::to_time_t(now);
        std::tm* local_time = std::localtime(&current_time);

        std::stringstream ss;
        ss << std::put_time(local_time, format.c_str());
        return ss.str();
    }
}